﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;

namespace Hr.Controllers
{
    public class xinchoubiaozhunController : Controller
    {
        private readonly ILogger<client1Controller> _logger;
        private readonly MyDbContext myDb;
        private readonly Isalary_standard_detailsServices d;
        private readonly Isalary_standardServices isalary;
        public xinchoubiaozhunController(ILogger<client1Controller> logger, MyDbContext myDb,Isalary_standard_detailsServices d,Isalary_standardServices isalary)
        {
            this.myDb = myDb;
            this.d = d;
            this.isalary = isalary;
            this._logger = logger;
        }
        //薪酬标准登记显示薪酬项目名称数据
        [HttpGet]
        public IActionResult salarystandard_register()
        {                  
            return View();
        }
        //薪酬项目登记
        [HttpPost]
        public async Task<ActionResult> salarystandard_register([FromForm] Esalary_standard test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDb.salary_standard.Add(test);
                    test.check_status = 1;
                    test.change_status = 1;
                    result = await myDb.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }
        [HttpPost]
        //点击复选框的薪酬项目的新增
        public async Task<IActionResult> xinchoufj(Esalary_standard_details sd)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDb.salary_standard_details.Add(sd);
                    
                    result = await myDb.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }
        //登记成功
        [HttpGet]
      public IActionResult salarystandard_register_success()
        {
            return View();
        }
        //薪酬标准登记复核
        [HttpGet]
        public IActionResult salarystandard_check_list()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> salarystandard_check_list(int currentpage)
        {
            FenYe ye = new FenYe()
            {
                CurrentPage = currentpage,
                PageSize = 3,
                Where = ""
            };
            Dictionary<string, object> di = new Dictionary<string, object>();
            List<salary_standard> list = await isalary.salary_standardSelectFenYe2(ye);
            di["dt"] = list;
            di["rows"] = ye.Rows;
            di["pages"] = ye.Pages;
            return Ok(di);
        }

        [HttpGet]
        //薪酬登记复核修改
        public IActionResult salarystandard_check(string id)
        {
            HttpContext.Session.SetString("id", id);
            return View();
        }

        [HttpGet]
        //薪酬登记复核修改根据id查询
        public async Task<IActionResult> salarystandard_check1()
        {
            string id = HttpContext.Session.GetString("id");
            Dictionary<string, object> di = new Dictionary<string, object>();
            salary_standard ss = await isalary.GetSelId(id);

            List<salary_standard_details> list = await d.Select();
            List<salary_standard_details> list2 = list.Where(e => e.standard_id == id).ToList();

            di["dt"] = ss;
            di["ds"] = list2;
            di["leg"] = list2.Count;
            return Ok(di);
        }

        //修改薪酬项目金额
        [HttpPost]
        public async Task<IActionResult> UpdXcxm(Esalary_standard_details ss)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDb.salary_standard_details.Attach(ss);
                myDb.Entry(ss).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDb.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }

        //薪酬登记复核修改
        [HttpPost]
        public async Task<IActionResult> salarystandard_check(Esalary_standard ss)
        {
                 int result;
            if (ModelState.IsValid)
            {
                myDb.salary_standard.Attach(ss);
                ss.change_status = 1;
                myDb.Entry(ss).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDb.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }
        //复核成功
        public IActionResult salarystandard_check_success()
        {
            return View();
        }
        //点击查询，薪酬标准登记查询
        [HttpGet]
        public IActionResult salarystandard_query_locate()
        {
            return View();
        }

        //薪酬标准登记查询
        [HttpGet]
        public IActionResult salarystandard_query_list(string bh, string gjz, DateTime start, DateTime end)
        {
            //HttpContext.Session.SetString("bh", bh);
            //HttpContext.Session.SetString("gjz", gjz);
            //HttpContext.Session.SetString("start", start.ToString());
            //HttpContext.Session.SetString("end", end.ToString());
            ViewBag.bh = bh;
            ViewBag.gjz = gjz;
            ViewBag.start = start;
            ViewBag.end = end;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> salarystandard_query_list1(int currentpage,string bh,string gjz,DateTime start,DateTime end)
        {
            //or regist_time  between '2020-10-30 00:00:00' and '2020-11-11 00:00:00'
            string where = $" check_status=0 and  regist_time  between '{start}' and '{end}' and standard_id like '%{bh}%' " +
                               $" and ( standard_name like '%{gjz}%' or  designer like '%{gjz}%' ) ";
          
            FenYe ye = new FenYe()
            {
                CurrentPage = currentpage,
                PageSize = 3,
                Where = where
            };
            Dictionary<string, object> di = new Dictionary<string, object>();
            List<salary_standard> list = await isalary.salary_standardFenYe(ye);
            di["dt"] = list;
            di["rows"] = ye.Rows;
            di["pages"] = ye.Pages;
            return Ok(di);
        }

        //薪酬标准登记查询
        [HttpGet]
        public IActionResult salarystandard_query(string id)
        {
            HttpContext.Session.SetString("id", id);
            return View();
        }
        [HttpGet]
        //薪酬标准登记查询
        public async Task<IActionResult> salarystandard_query1()
        {
            string id = HttpContext.Session.GetString("id");
            Dictionary<string, object> di = new Dictionary<string, object>();
            salary_standard ss = await isalary.GetSelId(id);

            List<salary_standard_details> list = await d.Select();
            List<salary_standard_details> list2 = list.Where(e => e.standard_id == id).ToList();

            di["dt"] = ss;
            di["ds"] = list2;
            di["time"] = ss.check_time;
            return Content(JsonConvert.SerializeObject(di));
        }

        //点击查询，薪酬标准登记变更
        [HttpGet]
        public IActionResult salarystandard_change_locate()
        {
            return View();
        }
        //薪酬标准登记变更
 
        [HttpGet]
        public IActionResult salarystandard_change_list(string bh, string gjz, DateTime start, DateTime end)
        {
            //HttpContext.Session.SetString("bh", bh);
            //HttpContext.Session.SetString("gjz", gjz);
            //HttpContext.Session.SetString("start", start.ToString());
            //HttpContext.Session.SetString("end", end.ToString());
            ViewBag.bh = bh;
            ViewBag.gjz = gjz;
            ViewBag.start = start;
            ViewBag.end = end;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> salarystandard_change_list1(int currentpage,string bh,string gjz,DateTime start,DateTime end)
        {
            //or regist_time  between '2020-10-30 00:00:00' and '2020-11-11 00:00:00'
            string where = $" change_status=1 and  regist_time  between '{start}' and '{end}' and standard_id like '%{bh}%' " +
                               $" and ( standard_name like '%{gjz}%' or  designer like '%{gjz}%' ) ";
            FenYe ye = new FenYe()
            {
                CurrentPage = currentpage,
                PageSize = 3,
                Where = where
            };
            Dictionary<string, object> di = new Dictionary<string, object>();
            List<salary_standard> list = await isalary.salary_standardFenYe2(ye);
            di["dt"] = list;
            di["rows"] = ye.Rows;
            di["pages"] = ye.Pages;
            return Ok(di);
        }

  

        [HttpGet]
        //薪酬登记复核修改
        public IActionResult salarystandard_change(string id)
        {
            HttpContext.Session.SetString("id", id);
            return View();
        }

        [HttpGet]
        //薪酬登记复核修改根据id查询
        public async Task<IActionResult> salarystandard_change1()
        {
            string id = HttpContext.Session.GetString("id");
            Dictionary<string, object> di = new Dictionary<string, object>();
            salary_standard ss = await isalary.GetSelId(id);

            List<salary_standard_details> list = await d.Select();
            List<salary_standard_details> list2 = list.Where(e => e.standard_id == id).ToList();

            di["dt"] = ss;
            di["ds"] = list2;
            di["leg"] = list2.Count;
            return Ok(di);
        }

        //修改薪酬项目金额
        [HttpPost]
        public async Task<IActionResult> UpdateXcxm(Esalary_standard_details ss)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDb.salary_standard_details.Attach(ss);
                myDb.Entry(ss).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDb.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }

        //薪酬登记复核修改
        [HttpPost]
        public async Task<IActionResult> salarystandard_change(Esalary_standard ss)
        {
            int result;
            if (ModelState.IsValid)
            {
                myDb.salary_standard.Attach(ss);
              //  ss.change_status = 1;
                ss.check_status = 1;
                myDb.Entry(ss).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDb.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }

        //薪酬标准登记变更提交成功
        [HttpGet]
        public IActionResult salarystandard_change_success()
        {
            return View();
        }
    }
}
